package com.wuliu.servlet;

import com.wuliu.dao.BusinessClassDAO;
import com.wuliu.dao.BusinessDAO;
import com.wuliu.dao.impl.BusinessClassDAOImpl;
import com.wuliu.dao.impl.BusinessDAOImpl;
import com.wuliu.model.Business;
import com.wuliu.model.BusinessClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/business/update")
public class AdminBusinessUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BusinessClassDAO businessClassDAO=new BusinessClassDAOImpl();
        List<BusinessClass> businessClasses =businessClassDAO.findAll();
        req.setAttribute("businessClasses",businessClasses);

        int id=Integer.valueOf(req.getParameter("id"));
        BusinessDAO businessDAO=new BusinessDAOImpl();
        Business business =businessDAO.findById(id);
        req.setAttribute("business",business);
        req.getRequestDispatcher("/WEB-INF/view/admin/business_update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=Integer.valueOf(req.getParameter("id"));
        String img=req.getParameter("img");
        String name=req.getParameter("name");
        String desc=req.getParameter("desc");
        String time=req.getParameter("time");
        int cid=Integer.valueOf(req.getParameter("cid"));
        Business business=new Business(id,img,name,desc,time,cid);
        BusinessDAO businessDAO=new BusinessDAOImpl();
        businessDAO.update(business);
        resp.sendRedirect("/admin/business");

    }
}
